package com.scalar.chiptrack.adaptor.telefunken;

import com.scalar.chiptrack.adaptor.chartered.CharteredWipStatus;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.MutableAttributeSet;
import java.util.ArrayList;
import java.io.FileReader;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 10 Mar, 2010
 * Time: 3:53:39 PM
 */
public class TeleFunkenHtmlWipParser extends HTMLEditorKit.ParserCallback
{
    private CharteredWipStatus wipStatus = null;
    private ArrayList m_fabList = new ArrayList();
    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;

    public TeleFunkenHtmlWipParser()
    {
         m_tableTagCount = 0;
         m_readyForParsing = false;
         m_cellOpen = false;
         m_cellIndex = -1;
         m_invalidRow = false;
    }

    public ArrayList<CharteredWipStatus> parse( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < m_fabList.size(); i++ )
        {
            wipStatus = ( CharteredWipStatus ) m_fabList.get( i );

        }
        return m_fabList;
    }

    public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
    {
        // FOR PARSING THE DOWNLOADED WIP STATUS FILE
        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;

            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 5 )
            {
                m_readyForParsing = true;

            }
            else if ( m_tableTagCount == 6 )
            {
                //this block is entered when the table containing data is parsed
                if ( ( wipStatus != null ) && ( m_invalidRow == false ) )
                {
                    m_fabList.add( wipStatus );
                }

                m_readyForParsing = false;
            }
        }

        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ) && ( a.getAttributeCount() == 0 ) )
            {

                if ( ( wipStatus != null ) )
                {
                    m_fabList.add( wipStatus );
                }

                wipStatus = new CharteredWipStatus();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }
    }

    public void handleText( char[] data, int pos )
    {
        if ( ( m_readyForParsing == false ) || ( wipStatus == null ) )
        {
            return;
        }

        String text = new String( data );

        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();

        }
        System.out.println("text = " + text);
        switch ( m_cellIndex )
        {
            case 0:
                if ( !( "".equals( text ) ) && !( "LOT".equals( text.trim() ) ) )
                {
                    wipStatus.setCustomerLot(text);
                }
                break;

            case 3:
                if ( !( "".equals( text ) ) && !( "CUSTOMER PRODUCT".equalsIgnoreCase( text ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setCustomerProduct( text );
                }
                break;

            case 4:
                if ( !( "".equals( text ) ) && !( "MASKSET".equalsIgnoreCase( text.trim() ) )  &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setBalanceMaskNo( text );
                }
                break;

            case 5:
                if ( !( "".equals( text ) ) && !( "QTY".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") )  )
                {
                    wipStatus.setWaferQty( text );
                }
                break;

            case 6:
                if ( !( "".equals( text ) ) && !( "TECHNOLOGY".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
                {
                    wipStatus.setProcessName( text );
                }
                break;

            case 7:
               if ( !( "".equals( text ) ) && !( "ROUTE".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ))
               {
                   wipStatus.setCurrentStage( text );
               }
               break;

            case 9:
                if ( !( "".equals( text ) ) && !( "LOTSTART".equalsIgnoreCase( text.trim() ) ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setDateIn( text );
                }
                break;

            case 10:
                if ( !( "".equals( text ) ) && !(text.trim().startsWith("SCHEDULED") ) &&  !( text.startsWith("&nbsp") ) )
                {
                    wipStatus.setExpectedDateOut( text );
                }
                break;

           default:
                break;
        }

    }

    // Resets the objects state
    public void reset()
    {
        m_tableTagCount = 0;
        m_readyForParsing = false;
        m_cellOpen = false;
        m_cellIndex = -1;
        m_invalidRow = false;
    }
}

